package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.entity.Invoice;
import com.api.syh56.shengyunhui.entity.vo.WAGParamVO;
import com.api.syh56.shengyunhui.entity.vo.WAGParmVOListAndInvoice;
import com.api.syh56.shengyunhui.mapper.InvoiceMapper;
import com.api.syh56.shengyunhui.service.AdminInvoiceService;
import com.api.syh56.shengyunhui.service.InvoiceService;
import com.api.syh56.shengyunhui.service.WayBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminInvoiceServiceImpl extends ServiceImpl<InvoiceMapper,Invoice> implements AdminInvoiceService {
    @Autowired
    private InvoiceService invoiceService;

    @Autowired
    private WayBillService wayBillService;

    @Override
    public List<Invoice> get() {
        List<Invoice> invoices = invoiceService.selectAll();
        return invoices;
    }


    @Override
    public WAGParmVOListAndInvoice getDetails(Long invoiceId) {
        Invoice invoice = invoiceService.selectBy(invoiceId);

        String widList = invoice.getWidList();
        Long[] wids = stringtoArray(widList);
        List<WAGParamVO> wagParamVOList = new ArrayList<>();
        for (Long wid : wids){
            WAGParamVO wagParamVO = wayBillService.convertToWAGParamVO(wid);
            wagParamVOList.add(wagParamVO);
        }

        WAGParmVOListAndInvoice wagParmVOListAndInvoice = new WAGParmVOListAndInvoice();
        wagParmVOListAndInvoice.setInvoice(invoice);
        wagParmVOListAndInvoice.setWagParamVOList(wagParamVOList);

        return wagParmVOListAndInvoice;
    }

    private Long[] stringtoArray(String widList) {
        String[] strings = widList.split(",");
        List<Long> longs = new ArrayList<>(strings.length);
        for (String str : strings){
            longs.add(Long.parseLong(str.trim()));
        }
        Long[] array = longs.toArray(new Long[0]);
        return array;
    }
}
